//
//  HomeTopBarView.swift
//  TimMusic
//
//  Created by nkl on 2024/10/8.
//

import Foundation
import KLExtension
import SnapKit
import UIKit

enum TopType {
    case addButton
    case back
    case tip
    case menu
}

class TopBarView: UIView {
    lazy var musicLogo: UIImageView = .simpleImage(imageName: "ic_music")
    lazy var hStack: UIStackView = {
        let stack : UIStackView = .hStack
        stack.spacing = 20
        return stack
    }()

    lazy var addButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_add"), for: .normal)
        return btn
    }()

    lazy var tipButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_tip_top"), for: .normal)
        return btn
    }()

    lazy var menuButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_menu_top"), for: .normal)
        return btn
    }()

    var types: [TopType] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(imgName:String ,types: [TopType]) {
        self.init(frame: .zero)
        self.musicLogo.image = UIImage.init(named: imgName)
        self.types = types
        addChildren()
        makeConstraints()
    }

    func addChildren() {
        addSubview(musicLogo)
        addSubview(hStack)
        for item in types {
            switch item {
            case .addButton:
                hStack.addArrangedSubview(addButton)
                addButton.snp.makeConstraints { make in
                    make.width.height.equalTo(24)
                }
            case .back:
                break
            case .tip:
                hStack.addArrangedSubview(tipButton)
                tipButton.snp.makeConstraints { make in
                    make.width.height.equalTo(24)
                }
            case .menu:
                hStack.addArrangedSubview(menuButton)
                menuButton.snp.makeConstraints { make in
                    make.width.height.equalTo(24)
                }
            }
        }
    }

    func makeConstraints() {
        musicLogo.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        hStack.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
